package com.ruoyi.framework.Reptilia;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;


@Component
public class mIkkdmCom {

    /*记录每一章有多少页*/
    public static int pubPage = 1;



    /*启动*/
    public  LinkedHashMap<String,String> getImg(String url){
        /**存放爬取的url*/
        LinkedHashMap<String,String> map = new LinkedHashMap<>();

        Document doc =  GetHtml(url);
        Elements a = doc.select("#list>li>a[href]");

        a.forEach(e->{
            String[] text = e.text().split(" ");
            String textNumber = text[1].indexOf("_")!= -1?text[1].split("_")[1]:"38";
            String key = text[0]+"&"+textNumber;
            String value = e.attr("href");
            map.put(key,value);
        });

        return  map;
    }

    /*获取图片连接与页数*/
    public  String returnUrl(String ur,int prPage,int i)throws IOException{
        System.err.println("获取url方法 =:"+"ur=:"+ur+"prPage=:"+prPage+"i=:"+i);

        Document doc =  GetHtml(ur);

        String str = doc.select("script[language=\"javascript\"]").toString();

        /*得到位置 坐标*/
        int num = str.indexOf("document.write(");

        /*document.write("<a href='/comiclist/950/18504/4.htm'><IMG SRC='"+m2007+"kuku7comic7/201010/20101002/G001/cccc_0020AJ.jpg'></a><span style='display:none'><img src='"+m201304d+"kuku7comic7/201010/201010*/
        /*通过拆分得到我们想要的内容*/
        String img = str.substring(num,num+150);
        int end = img.indexOf(".JPG") != -1?img.indexOf(".JPG"):img.indexOf(".jpg");
        /*再一次 拆分 拼装 才是我们想要的 图片url连接  最终的结果：https://s1.kukudm.com/kuku7comic7/201010/20101002/G001/cccc_0020AJ.jpg*/
        String cheng = "https://s1.kukudm.com/"+img.substring(img.indexOf("SRC=")+14,end+4);
        System.err.println("图片url===："+cheng);

        if(prPage == 1){
            String elspage = doc.select("ul[class=subNav]").toString();
            /*获取大概 包含 页数的标签*/
            int shPage = elspage.indexOf("<li>上一页</li>");
            //转换为整形 用于循环
            int page = Integer.valueOf(elspage
                    .substring(shPage+12,shPage+22)
                    //获取第二个数组 最终的结果 57~45，是个整数
                    .split("/")[1]
            );
            System.err.println("多少页===："+page);
            pubPage = page;
        }
        if(i == pubPage){
            pubPage = 1;
        }

        /*判断是否包含中文*/
        boolean bool = cheng.getBytes("utf-8").length>cheng.length()?true:false;

        if(bool){
            StringBuffer buffer = new StringBuffer();
            for (int j=0;j<cheng.length();j++){
                String cha  = cheng.substring(j,j+1);
                if(cha.getBytes("utf-8").length>2){
                    /*将中文编译为浏览器可以识别的*/
                    buffer.append(URLEncoder.encode(cha,"utf-8"));
                }else{
                    buffer.append(cha);
                }
            }
            System.err.println("有汉字编译后=："+buffer.toString());
            return buffer.toString().replace(" ","%20");
        }

        return cheng.replace(" ","%20");
    }


    /*爬取网页*/
    public static Document GetHtml(String url){
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(500000).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate, sdch").header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)")
                    .header("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36").get();
        } catch (IOException e) {

            e.printStackTrace();
            System.err.println(" e.printStackTrace();========================");
        }

        return doc;
    }

}
